//
//  WOAFNetWorkingHandle.h
//  MedicalAlarmSystem
//
//  Created by 吴欧 on 2017/8/31.
//  Copyright © 2017年 吴欧. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "WONetworkCache.h"

typedef NS_ENUM(NSUInteger, WONetworkStatusType) {
    /// 未知网络 -1
    WONetworkStatusUnknown,
    /// 无网络 0
    WONetworkStatusNotReachable,
    /// 手机网络 1
    WONetworkStatusReachableViaWWAN,
    /// WIFI网络 2
    WONetworkStatusReachableViaWiFi
};

NS_ASSUME_NONNULL_BEGIN

/**
 *  方便管理请求任务。执行取消，暂停，继续等任务.
 *  - (void)cancel，取消任务
 *  - (void)suspend，暂停任务
 *  - (void)resume，继续任务
 */

#pragma mark - typedef block
typedef void(^WOHttpRequestCache)(id responseCache);

typedef void(^WOHttpRquestSuccess)(id responseObject);

typedef void(^WOHttpRquestSuccessWithCache)(id responseObject, BOOL isCache);

typedef void(^WOHttpRequestFailed)(NSError *error);

/// 缓存的Block
typedef void(^WOHttpRequestCache)(id responseCache);

/// 上传或者下载的进度, Progress.completedUnitCount:当前大小 - Progress.totalUnitCount:总大小
typedef void (^WOHttpProgressBlock)(int64_t bytesProgress,int64_t totalBytesProgress);

/// 网络状态的Block
typedef void(^WONetworkStatusBlock)(WONetworkStatusType status);

typedef void(^movEncodeToMpegToolResultBlock)(NSURL *filePath);


@interface WOAFNetWorkingHandle : NSObject

#pragma mark - POST
/**
 POST请求
 
 @param baseUrl    服务地址，与suffixUrl拼接成完整url
 @param suffixUrl  url后缀，与baseUrl拼接成完整url
 @param parameter 请求参数
 @param success 请求成功的回调
 @param failure 请求失败的回调
 @return 返回的对象可取消请求,调用cancel方法
 */
+ (__kindof NSURLSessionTask *)POSTWithBaseUrl:(NSString *)baseUrl
                                     suffixUrl:(NSString *)suffixUrl
                                    parameters:(id)parameter
                                       success:(WOHttpRquestSuccess)success
                                       failure:(WOHttpRequestFailed)failure;

+ (__kindof NSURLSessionTask *)POSTNoHUDWithBaseUrl:(NSString *)baseUrl
                                     suffixUrl:(NSString *)suffixUrl
                                    parameters:(id)parameter
                                       success:(WOHttpRquestSuccess)success
                                       failure:(WOHttpRequestFailed)failure;

+ (__kindof NSURLSessionTask *)POSTWithBaseUrl:(NSString *)baseUrl
                                     suffixUrl:(NSString *)suffixUrl
                                    parameters:(id)parameter
                                         token:(NSString *)token
                                       success:(WOHttpRquestSuccess)success
                                       failure:(WOHttpRequestFailed)failure;


#pragma mark - key=value POST
// key=value形式传参数
+ (__kindof NSURLSessionTask *)POSTWithBaseUrlByKeyValue:(NSString *)baseUrl
                                          suffixUrl:(NSString *)suffixUrl
                                         parameters:(id)parameter
                                            success:(WOHttpRquestSuccess)success
                                                 failure:(WOHttpRequestFailed)failure;
// key=value形式传参数
+ (__kindof NSURLSessionTask *)POSTNoHUDWithBaseUrlByKeyValue:(NSString *)baseUrl
                                          suffixUrl:(NSString *)suffixUrl
                                         parameters:(id)parameter
                                            success:(WOHttpRquestSuccess)success
                                                      failure:(WOHttpRequestFailed)failure;

#pragma mark - GET
/**
 GET请求
 
 @param baseUrl    服务地址，与suffixUrl拼接成完整url
 @param suffixUrl  url后缀，与baseUrl拼接成完整url
 @param parameter 请求参数
 @param success 请求成功的回调
 @param failure 请求失败的回调
 @return 返回的对象可取消请求,调用cancel方法
 */
+ (__kindof NSURLSessionTask *)GETWithBaseUrl:(NSString *)baseUrl
                                    suffixUrl:(NSString *)suffixUrl
                                   parameters:(id)parameter
                                      success:(WOHttpRquestSuccess)success
                                      failure:(WOHttpRequestFailed)failure;

/**
 GET请求,无HUD
 
 @param baseUrl    服务地址，与suffixUrl拼接成完整url
 @param suffixUrl  url后缀，与baseUrl拼接成完整url
 @param parameter 请求参数
 @param success 请求成功的回调
 @param failure 请求失败的回调
 @return 返回的对象可取消请求,调用cancel方法
 */
+ (__kindof NSURLSessionTask *)GETNoHUDWithBaseUrl:(NSString *)baseUrl
                                    suffixUrl:(NSString *)suffixUrl
                                   parameters:(id)parameter
                                      success:(WOHttpRquestSuccess)success
                                      failure:(WOHttpRequestFailed)failure;

/**
 *  GET请求,自动缓存
 *
 *  @param baseUrl       请求地址
 *  @param suffixUrl  url后缀，与baseUrl拼接成完整url
 *  @param parameter     请求参数
 *  @param responseCache 缓存数据的回调
 *  @param success       请求成功的回调
 *  @param failure       请求失败的回调
 *
 *  @return 返回的对象可取消请求,调用cancel方法
 */
+ (__kindof NSURLSessionTask *)GETWithBaseUrl:(NSString *)baseUrl
                                    suffixUrl:(NSString *)suffixUrl
                                   parameters:(id)parameter
                                responseCache:(WOHttpRequestCache)responseCache
                                      success:(WOHttpRquestSuccessWithCache)success
                                      failure:(WOHttpRequestFailed)failure;

#pragma mark - upload file
/**
 *  上传文件
 *
 *  @param baseUrl 服务地址，与suffixUrl拼接成完整url
 *  @param suffixUrl url后缀，与baseUrl拼接成完整url
 *  @param parameters 请求参数
 *  @param name       文件对应服务器上的字段
 *  @param filePath   文件本地的沙盒路径
 *  @param progress   上传进度信息
 *  @param success    请求成功的回调
 *  @param failure    请求失败的回调
 *
 *  @return 返回的对象可取消请求,调用cancel方法
 */
+ (__kindof NSURLSessionTask *)uploadFileWithBaselURL:(NSString *)baseUrl
                                       suffixUrl:(nullable NSString *)suffixUrl
                                      parameters:(id)parameters
                                            name:(NSString *)name
                                        filePath:(NSString *)filePath
                                        progress:(WOHttpProgressBlock)progress
                                         success:(WOHttpRquestSuccess)success
                                         failure:(WOHttpRequestFailed)failure;


+ (__kindof NSURLSessionTask *)batchUploadFileWithBaseURL:(NSString *)baseUrl
                                             suffixUrl:(nullable NSString *)suffixUrl
                                            parameters:(id)parameters
                                                  name:(NSString *)name
                                                filePaths:(NSArray <NSURL *> *)filePaths
                                             fileNames:(NSArray<NSString *> *)fileNames
                                             mediaType:(NSString *)mediaType
                                              progress:(WOHttpProgressBlock)progress
                                               success:(WOHttpRquestSuccess)success
                                                  failure:(WOHttpRequestFailed)failure;

#pragma mark - upload image
/**
 *  上传单/多张图片
 *
 *  @param baseUrl 服务地址，与suffixUrl拼接成完整url
 *  @param suffixUrl url后缀，与baseUrl拼接成完整url
 *  @param parameters 请求参数
 *  @param name       图片对应服务器上的字段
 *  @param images     图片数组
 *  @param fileNames  图片文件名数组, 可以为nil, 数组内的文件名默认为当前日期时间"yyyyMMddHHmmss"
 *  @param imageScale 图片文件压缩比 范围 (0.f ~ 1.f)
 *  @param imageType  图片文件的类型,例:png、jpg(默认类型)....
 *  @param progress   上传进度信息
 *  @param success    请求成功的回调
 *  @param failure    请求失败的回调
 *
 *  @return 返回的对象可取消请求,调用cancel方法
 */
+ (__kindof NSURLSessionTask *)uploadImagesWithBaseURL:(NSString *)baseUrl
                                             suffixUrl:(nullable NSString *)suffixUrl
                                            parameters:(id)parameters
                                                  name:(NSString *)name
                                                images:(NSArray<UIImage *> *)images
                                             fileNames:(NSArray<NSString *> *)fileNames
                                            imageScale:(CGFloat)imageScale
                                             imageType:(NSString *)imageType
                                              progress:(WOHttpProgressBlock)progress
                                               success:(WOHttpRquestSuccess)success
                                               failure:(WOHttpRequestFailed)failure;

#pragma mark - 上传单张图片
+ (__kindof NSURLSessionTask *)uploadImagesWithBaseURL:(NSString *)baseUrl
                                             suffixUrl:(nullable NSString *)suffixUrl
                                            parameters:(id)parameters
                                                  name:(NSString *)name
                                                image:(UIImage *)image
                                             fileName:(NSString *)fileName
                                            imageScale:(CGFloat)imageScale
                                             imageType:(NSString *)imageType
                                              progress:(WOHttpProgressBlock)progress
                                               success:(WOHttpRquestSuccess)success
                                               failure:(WOHttpRequestFailed)failure;


#pragma mark - 上传单/多视频
+ (__kindof NSURLSessionTask *)uploadVideoWithBaseURL:(NSString *)baseUrl
                                             suffixUrl:(nullable NSString *)suffixUrl
                                            parameters:(id)parameters
                                                  name:(NSString *)name
                                                videoFilePaths:(NSArray <NSURL *> *)videoFilePaths
                                             fileNames:(NSArray<NSString *> *)fileNames
                                             mediaType:(NSString *)mediaType
                                              progress:(WOHttpProgressBlock)progress
                                               success:(WOHttpRquestSuccess)success
                                              failure:(WOHttpRequestFailed)failure;


#pragma mark - DownLoad
/**
 下载文件

 @param baseUrl 服务地址，与suffixUrl拼接成完整url
 @param suffixUrl url后缀，与baseUrl拼接成完整url
 @param saveToPath 存储地址
 @param progress 文件下载的进度信息
 @param success 下载成功的回调
 @param failure 下载失败的回调
 @return 返回的对象可取消请求,调用cancel方法
 */
+ (__kindof NSURLSessionTask *)downloadWithBaseUrlURL:(NSString *)baseUrl
                                       suffixUrl:(nullable NSString *)suffixUrl
                                      saveToPath:(NSString *)saveToPath
                                        progress:(WOHttpProgressBlock)progress
                                        success:(WOHttpRquestSuccess)success
                                         failure:(WOHttpRequestFailed)failure;

#pragma mark - 取消指定URL的HTTP请求

/// 取消所有HTTP请求
+ (void)cancelAllRequest;

+ (void)cancelRequestWithURL:(NSString *)URL;

#pragma mark - 监控当前网络变化
/**
 实时获取网络状态

 @param networkStatus 通过Block回调实时获取(此方法可多次调用)
 */
+ (void)networkStatusWithBlock:(WONetworkStatusBlock)networkStatus;

/**
   监控当前网络变化
 */
+ (void)checkNetworkStatus;

/// 有网YES, 无网:NO
+ (BOOL)isNetwork;

/// 手机网络:YES, 反之:NO
+ (BOOL)isWWANNetwork;

/// WiFi网络:YES, 反之:NO
+ (BOOL)isWiFiNetwork;

@end

NS_ASSUME_NONNULL_END
